﻿using MobishopManagement.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobishopManagement.Controller
{
    public class AccountCtrl
    {
        public static List<Account> GetList()
        {
            using (MobiShopDBOEntities en = new MobiShopDBOEntities())
            {
                return en.Accounts.ToList();
            }
        }

        public static bool Add(Account obj)
        {
            try
            {
                using (MobiShopDBOEntities en = new MobiShopDBOEntities())
                {
                    if (en.Accounts.Where(x => x.AccUsername == obj.AccUsername).FirstOrDefault() != null)
                        return false;
                    en.sp_NewAccount(obj.AccUsername, obj.AccPassword, obj.AccType);
                    return true;
                }
            }
            catch (DbEntityValidationException ex)
            {
                return false;
            }
        }

        public static bool Delete(int AccID)
        {
            try
            {
                using (MobiShopDBOEntities en = new MobiShopDBOEntities())
                {
                    Account acc = en.Accounts.Where(x => x.AccID == AccID).FirstOrDefault();
                    en.Accounts.Remove(acc);
                    if (en.SaveChanges() > 0)
                        return true;
                    else return false;
                }
            }
            catch (DbEntityValidationException ex)
            {
                return false;
            }
        }

        public static bool Update(Account obj)
        {
            try
            {
                using (MobiShopDBOEntities en = new MobiShopDBOEntities())
                {
                    Account acc = en.Accounts.Where(x => x.AccID == obj.AccID).FirstOrDefault();
                    acc.AccUsername = obj.AccUsername;
                    acc.AccPassword = obj.AccPassword;
                    acc.AccType = obj.AccType;

                    if (en.SaveChanges() > 0)
                        return true;
                    else return false;
                }
            }
            catch (DbEntityValidationException ex)
            {
                return false;
            }
        }
    }
}